<template>
  <div style="background: #fff">
    <home-header></home-header>
    <home-swiper :swiperImg="swiperImg"></home-swiper>
    <home-icons-one :icons="icons"></home-icons-one>
    <home-swiper-two :swiperTwo="swiperTwo"></home-swiper-two>
    <home-icons-two :icons="iconsTwo"></home-icons-two>
    <div class="ad" v-for="item of listAd.slice(0, 1)" :key="item.id">
      <router-link :to="item.url">
        <img class="img-width-max" :src="item.path" alt="">
      </router-link>
    </div>
    <home-Brand></home-Brand>
    <home-sellhot :list="hotList" :banner="bannerHot"></home-sellhot>
    <home-daily-pro :listDaily="dailyList" :banner="bannerNew" :bannerClear="bannerClear" :listSale="saleList"></home-daily-pro>
    <home-guess :list="guessList"></home-guess>
    <public-top></public-top>
    <public-menu></public-menu>
  </div>
</template>

<script>
import axios from 'axios'
import wx from 'weixin-js-sdk'
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIconsOne from './components/IconsOne'
import HomeSwiperTwo from './components/SwiperTwo'
import HomeIconsTwo from './components/IconsTwo'
import HomeBrand from './components/Brand'
import HomeSellhot from './components/Sellhot'
import HomeDailyPro from './components/DailyPro'
import HomeGuess from './components/Guess'
import PublicMenu from '../public/Menu'
import PublicTop from '../public/Top'

export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIconsOne,
    HomeSwiperTwo,
    HomeIconsTwo,
    HomeBrand,
    HomeSellhot,
    HomeDailyPro,
    HomeGuess,
    PublicTop,
    PublicMenu
  },
  data () {
    return {
      swiperImg: [],
      dailyList: [],
      saleList: [],
      hotList: [],
      swiperTwo: [],
      listAd: [],
      guessList: [],
      bannerHot: {},
      bannerNew: {},
      bannerClear: {},
      icons: [{
        id: '001',
        name: '教我赚钱',
        link: '/money',
        imgUrl: 'static/icon/money.png'
      }, {
        id: '002',
        name: '分享有礼',
        link: 'share',
        imgUrl: 'static/icon/share.png'
      }, {
        id: '003',
        name: '签到领积分',
        link: '/sign',
        imgUrl: 'static/icon/sign.png'
      }, {
        id: '004',
        name: '申请直播间',
        link: 'evenyou',
        imgUrl: 'static/icon/live.png'
      }, {
        id: '005',
        name: '申请供应商',
        link: '/supply',
        imgUrl: 'static/icon/supply.png'
      }, {
        id: '006',
        name: '点我种草',
        link: '/grass',
        imgUrl: 'static/icon/grass.png'
      }, {
        id: '007',
        name: '点我银花',
        link: '/SilverFlower',
        imgUrl: 'static/icon/flower.png'
      }, {
        id: '008',
        name: '点我金花',
        link: '/GoldenFlower',
        imgUrl: 'static/icon/flower.png'
      }, {
        id: '009',
        name: '点我牡丹花',
        link: '/Peony',
        imgUrl: 'static/icon/peony.png'
      }, {
        id: '010',
        name: '槿萱家',
        link: '/list?tag=槿萱家',
        imgUrl: 'static/icon/brand.png'
      }],
      iconsTwo: [{
        id: '001',
        name: '积分兑换',
        link: '/listpoint',
        imgUrl: 'static/icon/integral.png'
      }, {
        id: '002',
        name: '卿点好物',
        link: '/listHaowu',
        imgUrl: 'static/icon/second.png'
      }, {
        id: '003',
        name: '最划算',
        link: '/list?customer_id=promotion',
        imgUrl: 'static/icon/sale.png'
      }, {
        id: '004',
        name: '便民服务',
        link: '/',
        imgUrl: 'static/icon/convenient.png'
      }, {
        id: '005',
        name: '爱心公益',
        link: '/',
        imgUrl: 'static/icon/love.png'
      }, {
        id: '006',
        name: '全球购',
        link: '/list?customer_id=global',
        imgUrl: 'static/icon/global.png'
      }, {
        id: '007',
        name: '台湾美麦',
        link: '/list?customer_id=taiwan',
        imgUrl: 'static/icon/taiwan.png'
      }, {
        id: '008',
        name: '每日生鲜',
        link: '/list?tag=每日生鲜',
        imgUrl: 'static/icon/fresh.png'
      }, {
        id: '009',
        name: '我爱我家',
        link: '/list?customer_id=lovehome',
        imgUrl: 'static/icon/lovehome.png'
      }, {
        id: '010',
        name: '萌宠到家',
        link: '/pet',
        imgUrl: 'static/icon/pet.png'
      }]
    }
  },
  methods: {
    getTopInfo () {
      let code = ''
      if (this.$store.state.userId) {
        code = JSON.parse(this.$store.state.token)
        this.$router.push({path: '/', query: {shareId: code.personalCode}})
      }
      // console.log(this.$route)
      // if (this.$route.query.shareId && !this.$store.state.userId) {
      //   console.log('info', this.$route.query.shareId)
      //   console.log('info', this.$store.state.userId)
      //   console.log('info', this.$store.state.userId)
      //   this.$router.push({
      //     path: '/register',
      //     query: { shareId: this.$route.query.shareId }
      //   })
      // }
      // 顶部轮播
      axios.get('/meimai/interface/ad/getTop').then((res) => {
        let datas = res.data
        if (datas.code === 0) {
          this.swiperImg = datas.data
        }
      })
      axios.get('/meimai/interface/goods/getPromotion').then((res) => {
        let datas = res.data
        if (datas.code === 0) {
          // console.log('最划算', datas)
        }
      })
      // 中部轮播
      axios.get('/meimai/interface/ad/getMiddle').then((res) => {
        let datas = res.data
        if (datas.code === 0) {
          this.swiperTwo = datas.data
        }
      })
      // 热销优品
      axios.get('/meimai/interface/goods/getHot').then((res) => {
        let datas = res.data
        if (datas.code === 0 && datas.data) {
          this.hotList = datas.data.proList
        }
      })
      axios.get('/meimai/interface/goods/getNew').then((res) => {
        let datas = res.data
        if (datas.code === 0 && datas.data) {
          this.dailyList = datas.data.proList
        }
      })
      axios.get('/meimai/interface/goods/getClearance').then((res) => {
        let datas = res.data
        // console.log(datas)
        if (datas.code === 0 && datas.data) {
          this.saleList = datas.data
        }
      })
      axios.get('/meimai/interface/goods/getRelated').then((res) => {
        let datas = res.data
        // console.log(datas)
        if (datas.code === 0 && datas.data) {
          this.guessList = datas.data
        }
      })
      // 首页游戏广告图
      axios.get('/meimai/interface/ad/getAd').then((res) => {
        let datas = res.data
        // console.log(datas)
        if (datas.code === 0 && datas.data) {
          this.listAd = datas.data
        }
      })
      axios.get('/meimai/interface/ad/getHotAd').then((res) => {
        let datas = res.data
        // console.log(datas)
        if (datas.code === 0 && datas.data) {
          this.bannerHot = datas.data
        }
      })
      axios.get('/meimai/interface/ad/getNewAd').then((res) => {
        let datas = res.data
        // console.log(datas)
        if (datas.code === 0 && datas.data) {
          this.bannerNew = datas.data
        }
      })
      axios.get('/meimai/interface/ad/getClearAd').then((res) => {
        let datas = res.data
        // console.log(datas)
        if (datas.code === 0 && datas.data) {
          this.bannerClear = datas.data
        }
      })
    }
  },
  mounted () {
    this.getTopInfo()
    axios.get('/meimai/interface/wx/getTicketInfo?url=' + encodeURIComponent(window.location.href.split('#')[0])).then((res) => {
      // alert(window.location.href.split('#')[0])
      wx.config({
        debug: false,
        appId: res.data.appid,
        timestamp: res.data.timestamp,
        nonceStr: res.data.noncestr,
        signature: res.data.signature,
        jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline']
      })
    })
    wx.ready(function () {
      // 分享给朋友
      // let code = ''
      // if (this.$store.state.userId) {
      //   let codeData = ''
      //   codeData = JSON.parse(this.$store.state.token)
      //   code = codeData.personalCode
      // }
      wx.onMenuShareAppMessage({
        title: '美麦商城', // 分享标题
        desc: '美麦商城官方首页', // 分享描述
        link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: 'http://bjmeimai.aefyc.com/static/img/logo.png', // 分享图标
        success: function (res) {
          // 用户确认分享后执行的回调函数
          alert('分享成功')
        },
        cancel: function (res) {
          // 用户取消分享后执行的回调函数
          alert('取消分享')
        }
      })
      // 分享到朋友圈
      wx.onMenuShareTimeline({
        title: '美卖商城', // 分享标题
        link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: 'http://bjmeimai.aefyc.com/static/img/logo.png', // 分享图标
        success: function (res) {
          // 用户确认分享后执行的回调函数
          alert('分享成功!')
        },
        cancel: function (res) {
          // 用户取消分享后执行的回调函数
          alert('取消分享')
        }
      })
    })
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/mixins.styl'
</style>
